<?php 
/*********************************************************************************
easyZenImport v.8b - Easy, Ajax Driven Importing Front-End For EasyPopulate 
Copyright (C) 11-02-06 Nick Van Dorsten
Author: Nick Van Dorsten <nickvd@gmail.com>

This library is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2.1 of the License, or (at your option) any later
version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*********************************************************************************/
class imageResize {
   var $x_max_size = 640;
   var $y_max_size = 480;
   var $x_max_thumb_size = 320;
   var $y_max_thumb_size = 240;
   var $quality = 70;
   var $x_size;
   var $y_size;
   var $larger_dim;
   var $larger_curr_value;
   var $larger_dim_value;
   var $larger_dim_thumb_value;
   
   function resize($from,$to) {
      $this->check_dimensions($from); // check which size is longer then the max value
      if ($this->larger_curr_value > $this->x_max_size) { //is image larger than defined max?
         $this->resizeSave($from, $to, $this->larger_dim_thumb_value, $this->quality);
      }
   }
   function thumb($from,$to) {
      $this->check_dimensions($from);
      $max = "{$this->larger_dim}_max_thumb_size";
      if ($this->larger_curr_value > $this->$max) { //is image larger than defined max?
         $this->resizeSave($from, $to, $this->larger_dim_thumb_value, $this->quality);
      }
   }
   function get_img_size($file) {
      $img_size = getimagesize($file);
      $this->x_size = $img_size[0];
      $this->y_size = $img_size[1];
   }
   function check_dimensions($filename) {
      $this->get_img_size($filename);
      $x_check = $this->x_size - $this->x_max_size;
      $y_check = $this->y_size - $this->y_max_size;
      if ($x_check < $y_check) {
         $this->larger_dim = "y";
         $this->larger_curr_value = $this->y_size;
         $this->larger_dim_value = $this->y_max_size;
         $this->larger_dim_thumb_value = $this->y_max_thumb_size;
      } else {
         $this->larger_dim = "x";
         $this->larger_curr_value = $this->x_size;
         $this->larger_dim_value = $this->x_max_size;
         $this->larger_dim_thumb_value = $this->x_max_thumb_size;
      }
   }
   function resizeSave($file_name_src, $file_name_dest, $target_size, $quality = 80) {
      $size = getimagesize($file_name_src);
      if ($this->larger_dim == "x") {
         $w = number_format($target_size, 0, ',', '');
         $h = number_format(($size[1]/$size[0])*$target_size,0,',','');
      } else {
         $w = number_format(($size[0]/$size[1])*$target_size,0,',','');
         $h = number_format($target_size, 0, ',', '');
      }
      $dest = imagecreatetruecolor($w, $h);
      $src = imagecreatefromjpeg($file_name_src);
      imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
      imagejpeg($dest, $file_name_dest, $quality);
   }
}
?>